#include "Image.h"
#include <eggx.h>
#include <eggx_color.h>
#include <iostream>

Image::Image() {
    this->width = 800;
    this->height = 800;

    this->win = gopen(this->width, this->height);
    setup();
}

void Image::setup() {
    winname(this->win, "Diploma");
    gsetbgcolor(this->win, "white");
    layer(this->win, 1, 0);
    gclr(this->win);
    newcolor(win, "#000000");
}

Image::~Image() {
}

int Image::get_win() const {
    return this->win;
}

int Image::draw(const Surface& value, const int& type) {
    gclr(this->win);
    int r, g, b;
    long unsigned int surf_dim = value.get_earth().size();
    double min_height = value.min_height();
    double max_height = value.max_height();
    double min_condition = value.min_condition();
    double max_condition = value.max_condition();
    if (type == DS9_BLUE) {
        for (long unsigned int i = 0; i != surf_dim; i++) {
            for (long unsigned int j = 0; j != surf_dim; j++) {
                makecolor(type, min_condition, max_condition, value.get_earth()[i][j].get_condition(), &r, &g, &b);
                newrgbcolor(win, r, g, b);
                fillrect(win, 50.0 + 10.0 * static_cast<double>(i), 50.0 + 10.0 * static_cast<double>(j), 10.0, 10.0);
            }
        }
        copylayer(this->win, 0, 1);
        return 2;
    }
    else if(type == DS9_GREEN) {
        for (long unsigned int i = 0; i != surf_dim; i++) {
            for (long unsigned int j = 0; j != surf_dim; j++) {
                makecolor(type, min_height, max_height, value.get_earth()[i][j].get_hight(), &r, &g, &b);
                newrgbcolor(win, r, g, b);
                fillrect(win, 50.0 + 10.0 * static_cast<double>(i), 50.0 + 10.0 * static_cast<double>(j), 10.0, 10.0);
            }
        }
        copylayer(this->win, 0, 1);
        return 1;
    }
    else {
        return 0;
    }
}
